package com.whynot.frame.basic.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * 描述: Swagger在SpringBoot下的基础配置:
 * 作者: chenyao
 * 日期: 2018-01-30.
 * 项目名称: curtain-whynot-basic
 * 版本: 1.0.0
 * JDK: since 1.8
 */
@Configuration
@EnableWebMvc
@EnableSwagger2
public class Swagger2Config {

    @Value("${swagger2.basePackge:com.whynot.frame}")
    String basePackge;
    @Value("${swagger2.title:curtain框架api}")
    String title;
    @Value("${swagger2.description:SpringBoot与Vue.js结合的基础框架接口}")
    String description;
    @Value("${swagger2.version:1.0.0}")
    String version;
    @Value("${swagger2.termsOfServiceUrl:https://my.oschina.net/u/2472104}")
    String termsOfServiceUrl;
        @Value("${swagger2.contactName:13026167827@sina.cn}")
    String contactName;
    @Value("${swagger2.license:curtain}")
    String license;
    @Value("${swagger2.licenseUrl:https://my.oschina.net/u/2472104}")
    String licenseUrl;

    @Bean
    public Docket docket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        //添加head参数start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        //添加head参数end

        ApiInfo apiInfo = new ApiInfo(
                title,
                description,
                version,
                termsOfServiceUrl,
                contactName,
                license,
                licenseUrl);
        docket.apiInfo(apiInfo).select()
                .apis(RequestHandlerSelectors.basePackage(basePackge))
                .paths(PathSelectors.any())
                .build()
                .securityContexts(securityContexts())
                .securitySchemes(securitySchemes());
                //.globalOperationParameters(pars);//每个请求都加上固定参数
        return docket;
    }

    /**
     * 设置全局token授权入口
     * http://www.leftso.com/blog/393.html
     * @return
     */
    private List<ApiKey> securitySchemes() {
        return newArrayList(
                new ApiKey("Authorization", "Authorization", "header"));
    }

    /**
     * 安全上下文
     * @return
     */
    private List<SecurityContext> securityContexts() {
        return newArrayList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!/login).*$"))
                        .build()
        );
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(
                new SecurityReference("Authorization", authorizationScopes));
    }

}
